package com.mimiwang.animal.service.Imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.mimiwang.animal.entity.Animal;
import com.mimiwang.animal.mapper.AnimalMapper;
import com.mimiwang.animal.mapper.AnimalTypeMapper;
import com.mimiwang.animal.service.IAnimalService;
import com.mimiwang.animal.vo.AnimalInfoVo;
import com.mimiwang.common.entity.school.School;
import com.mimiwang.common.entity.school.SchoolArea;
import com.mimiwang.common.entity.school.SchoolCampus;
import com.mimiwang.common.utils.IPUtil;
import com.mimiwang.common.vo.school.PageConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/4/19 15:21
 */

@Service
@Slf4j
public class AnimalServiceImpl implements IAnimalService {


    @Autowired
    AnimalMapper animalMapper;

    @Autowired
    AnimalTypeMapper animalTypeMapper;


    @Autowired
    RedisTemplate<String,Object> redisTemplate;



    private List<AnimalInfoVo> generateAnimalInfoVoList(List<Animal> animals){
        List<AnimalInfoVo> lst = new ArrayList<>();
//      遍历animals查找animal的类型和学校信息
        for (Animal animal : animals) {
//            包装单个
            AnimalInfoVo animalInfoVo = this.generateAnimalInfoVo(animal);
//            加入数组
            lst.add(animalInfoVo);
        }
        return lst;
    }

    private AnimalInfoVo generateAnimalInfoVo(Animal animal){
        //        TODO 通过animal查找animal的类型和学校、校区、区域
//        AnimalInfoVo animalInfoVo = new AnimalInfoVo();
//        animalInfoVo.setAnimal(animal);
//        animalInfoVo.setSchool(new School());
//        animalInfoVo.setCampus(new SchoolCampus());
//        animalInfoVo.setArea(new SchoolArea());
////        设置类型
//        animalInfoVo.setAnimalType(animalTypeMapper.selectAnimalTypeById(animal.getTypeId()));



        return  animalMapper.selectAnimalVoById(animal.getId());
    }

    @Override
    public PageInfo<AnimalInfoVo> selectAllAnimal(PageConfig pageConfig) {
        PageHelper.startPage(pageConfig);
//        查询所有animal
        List<Animal> animals = animalMapper.selectAllAnimal();

//        将animalList转化成    vo list
        List<AnimalInfoVo> lst = this.generateAnimalInfoVoList(animals);

        return new PageInfo<>(lst);
    }


    @Cacheable(value = "animal" , key = "#id")
    @Override
    public AnimalInfoVo selectAnimalById(long id) {

        return this.animalMapper.selectAnimalVoById(id);
    }

    @Override
    public PageInfo<AnimalInfoVo> selectAnimalListBySchool(long schoolId, long  campusId,long areaId ,PageConfig pageConfig) {
        PageHelper.startPage(pageConfig);
        List<Animal> animals = animalMapper.selectAllAnimalBySchool(schoolId,campusId ,areaId );

        return new PageInfo<>(this.generateAnimalInfoVoList(animals));
    }

    @Override
    public int selectAnimalNumBySchool(School school, SchoolCampus campus) {
        animalMapper.selectAnimalNumBySchool(school.getId(),campus.getId());
        return 0;
    }

    @Override
    public List<Object> selectAnimalListByRedisKey(String key) {

        List<Object> range = redisTemplate.opsForList().range(key, 0, -1);
        return range;
    }

    @Override
    public List<AnimalInfoVo> selectAroundAnimalList(String ip) {
        IPUtil.Address address=null;

        List<AnimalInfoVo> lst =new ArrayList<>();
//        搜索半径
        double distance=100;
        try {
            address = IPUtil.getAddress(ip);
//            纬度
            double latitude = address.getLatitude();
//            经度
            double longitude = address.getLongitude();
//            查附近学校
            GeoResults<RedisGeoCommands.GeoLocation<Object>> school = redisTemplate.opsForGeo()
                    .radius("school",
                            new Circle(
                                    new Point(longitude, latitude),
                                    new Distance(distance, RedisGeoCommands.DistanceUnit.KILOMETERS)
                            )
                    );

//            根据学校查动物

            school.forEach(item->{
                PageConfig pageConfig = new PageConfig(1, 5);
                School sc =(School) item.getContent().getName();

                PageInfo<AnimalInfoVo> animalInfoVoPageInfo = this.selectAnimalListBySchool(sc.getId(), 0,0,pageConfig );
                if(animalInfoVoPageInfo.getList().size()>0){
                    lst.addAll(animalInfoVoPageInfo.getList());
                }

            });

        } catch (IOException e) {
            e.printStackTrace();
        } catch (GeoIp2Exception e) {
            e.printStackTrace();
        }
        return lst;
    }
}
